Skip to content

Conversation

satya164
Copy link
Member

@satya164 satya164 commented Mar 26, 2025

There's a possibility of dual package hazard with the current setup. Metro enables exports support from 0.82.0, so this can become a problem for some packages.

So this drops the dual package setup in favor of an ESM-only setup. To make a similar change in your package, apply the following change to your entrypoints:

-  "main": "./lib/commonjs/index.js",
+  "main": "./lib/module/index.js",a
-  "types": "./lib/typescript/commonjs/src/index.d.ts",
   "exports": {
     ".": {
-      "import": {
-        "types": "./lib/typescript/module/src/index.d.ts",
-        "default": "./lib/module/index.js"
-      },
-      "require": {
-        "types": "./lib/typescript/commonjs/src/index.d.ts",
-        "default": "./lib/commonjs/index.js"
-      }
+      "types": "./lib/typescript/src/index.d.ts",
+      "default": "./lib/module/index.js"
     }
   },

Also, remove the commonjs target from the react-native-builder-bob field in your package.json or bob.config.js or bob.config.mjs:

"react-native-builder-bob": {
  "source": "src",
  "output": "lib",
  "targets": [
    ["module", { "esm": true }],
-   ["commonjs", { "esm": true }]
    "typescript",
  ]
}

With this change, Jest will break for the consumers of your libraries due to the usage of ESM syntax. So they may need to update their Jest configuration to transform your library:

module.exports = {
  preset: 'react-native',
+ transform: {
+   'node_modules/(your-library|another-library)': 'babel-jest',
+ },
};

If consumers of your library are using it in NodeJS in a CommonJS environment, they'll need to use at least NodeJS v20.19.0 to be able to synchronously require your library.

You can read more at our ESM support docs.

@satya164 satya164 force-pushed the @satya164/drop-commonjs branch 2 times, most recently from 3352d8f to 94fd772 Compare March 27, 2025 15:46
@satya164 satya164 force-pushed the @satya164/drop-commonjs branch from 94fd772 to 18126b9 Compare March 27, 2025 15:54
@satya164 satya164 force-pushed the @satya164/drop-commonjs branch 5 times, most recently from 7a65f9d to 5c773be Compare April 4, 2025 21:04
@satya164 satya164 force-pushed the @satya164/drop-commonjs branch from 5c773be to 3324a3f Compare April 4, 2025 21:12
@satya164 satya164 marked this pull request as ready for review April 4, 2025 21:23
@satya164 satya164 merged commit f11b666 into main Apr 4, 2025
22 checks passed
@satya164 satya164 deleted the @satya164/drop-commonjs branch April 4, 2025 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant